package com.hmsm.sys.daoImpl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.hmsm.server.base.BaseDao;
import com.hmsm.sys.dao.LogInfoDao;
import com.hmsm.sys.po.LogInfo;

@Repository
public class LogInfoDaoImpl extends BaseDao<LogInfo, Integer> implements
		LogInfoDao {
	
	/**
	 * 根据条件分页获取日志信息
	 * @param beginTime 开始时间 
	 * @param endTime 结束时间
	 * @param userName 用户
	 * @param type 日志类型
	 * @param pageIndex 当前页
	 * @param pageSize 每页记录条数
	 * @return 分页结果
	 * @author 何杰
	 */
	@SuppressWarnings("unchecked")
	public List<LogInfo> getInfoList(final Date beginTime,final Date endTime, final Integer type,final String userName,final Integer pageIndex,final Integer pageSize){
		Object obj = getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria cr = session.createCriteria(LogInfo.class);
				if(beginTime != null){
					cr.add(Restrictions.ge("createDate", beginTime));
				}
				if (endTime != null) {
					cr.add(Restrictions.le("createDate", endTime));
				}
				if (userName != null && !userName.equals("")) {
					cr.add(Restrictions.like("userName", userName, MatchMode.ANYWHERE));
				}
				if (type != null) {
					cr.add(Restrictions.eq("type", type));
				}
				return cr.addOrder(Order.desc("createDate")).setFirstResult((pageIndex-1)*pageSize).setMaxResults(pageSize).list();
			}
		});
		return (List<LogInfo>) obj;
	}
	/**
	 * 根据条件获取日志记录条数
	 * @param beginTime
	 * @param endTime
	 * @param userName
	 * @param type
	 * @return
	 * @author 何杰
	 */
	@SuppressWarnings("unchecked")
	public int getLogInfoCount(final Date beginTime,final Date endTime,final String userName, final Integer type){
		Object obj = getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria cr = session.createCriteria(LogInfo.class);
				if(beginTime != null){
					cr.add(Restrictions.ge("createDate", beginTime));
				}
				if (endTime != null) {
					cr.add(Restrictions.lt("createDate", endTime));
				}
				if (userName != null && !userName.equals("")) {
					cr.add(Restrictions.like("userName", userName, MatchMode.ANYWHERE));
				}
				if (type != null) {
					cr.add(Restrictions.eq("type", type));
				}
				return cr.setProjection(Projections.rowCount()).uniqueResult();
			}
		});
		if (obj != null) {
			return Integer.parseInt(obj.toString());
		}
		return 0;
	}
	/**
	 * 
	 *  @Description:是否当天第一次登录
	 *  @MethodName :isFirstLoginNowDay
	 *  @Author     :zhangshuai
	 *  @CreateDate :2013-7-12 下午03:37:46 
	 *  @param userid
	 *  @param time
	 *  @return
	 *
	 */
	public boolean isFirstLoginNowDay(int userid,String time) {
		String sql="SELECT * FROM log_info WHERE user_id= "+userid+" AND CREATE_DATE LIKE '%"+time+"%' LIMIT 0, 2;"; 
		List<LogInfo> list= this.queryBySql(sql);
		if(list!=null && list.size()>1){
			return false;
		}else {
			return true;//1表示当天第一次登陆
		}
		
	}
}
